package main

import (
	"bytes"
	"encoding/binary"
	"fmt"
	"strconv"
)

func main() {
	var mydata int = 10
	//将数值转换成字符串
	var str string = strconv.Itoa(mydata)
	fmt.Println("数值转换为字母:" + str)
	//字符串转换成数值
	var strNum = "1234"
	newNum, _ := strconv.Atoi(strNum)
	fmt.Println("字符串转换成数值:", newNum)
	num10, _ := strconv.ParseInt(strNum, 10, 32) //将一个字符串转换成一个十进制 32位的整数
	fmt.Println("将一个字符串转换成一个十进制 32位的整数", num10)  //
	fmt.Println("将整数转化成浮点数：", float64(num10))

	var a int = 23
	bys := IntToBytes(a)
	fmt.Println("转换后：", BytesToInt(bys))
}

//整形转换成字节
func IntToBytes(n int) []byte {
	tmp := int32(n)
	bytesBuffer := bytes.NewBuffer([]byte{})
	binary.Write(bytesBuffer, binary.BigEndian, tmp)
	return bytesBuffer.Bytes()
}

//字节转换成整形
func BytesToInt(b []byte) int {
	bytesBuffer := bytes.NewBuffer(b)
	var tmp int32
	binary.Read(bytesBuffer, binary.BigEndian, &tmp)
	return int(tmp)
}
